/****** Object:  UserDefinedFunction [dbo].[SplitCVS]    Script Date: 10/17/2007 13:14:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO





CREATE FUNCTION [dbo].[SplitCVS]
    (
      @OrderList VARCHAR(MAX)
    )
RETURNS @ParsedList TABLE ( OrderID VARCHAR(max) )
AS BEGIN
    DECLARE @OrderID VARCHAR(max),
        @Pos VARCHAR(max)
    SET @OrderList = LTRIM(RTRIM(@OrderList)) + ','
    SET @Pos = CHARINDEX(',', @OrderList, 1)
    IF REPLACE(@OrderList, ',', '') <> '' 
        BEGIN
            WHILE @Pos > 0
                BEGIN
                    SET @OrderID = LTRIM(RTRIM(LEFT(@OrderList, @Pos - 1)))
                    IF @OrderID <> '' 
                        BEGIN
                            INSERT  INTO @ParsedList ( OrderId )
                            VALUES  (
                                      CAST(@OrderID AS VARCHAR(max)) 
                                    ) --Use Appropriate conversion
                        END
                    SET @OrderList = RIGHT(@OrderList, LEN(@OrderList) - @Pos)
                    SET @Pos = CHARINDEX(',', @OrderList, 1)
                END
        END	
    RETURN
   END





